<template>
  <el-tabs type="border-card">
    <el-tab-pane label="内部生产">
      <el-table
          :data="toolOrderItemManagementVOS" border
          style="width: 100%" size="mini" :span-method="objectSpanMethod">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column label="图号" prop="drawingNumber" width="250px"></el-table-column>
        <el-table-column label="订单号" prop="sn"></el-table-column>
        <el-table-column label="交货时间" prop="requestDeliveryTime"></el-table-column>
        <el-table-column label="数量" prop="number"></el-table-column>
        <el-table-column label="总数量" prop="totalNumber">
          <template slot-scope="scope">
            {{ scope.row.totalNumber }}
          </template>
        </el-table-column>
        <el-table-column label="生产批次" prop="productionBatch">
          <template slot-scope="scope">
            <el-input size="mini" v-model="scope.row.productionBatch"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="领料" prop="lingliao"></el-table-column>
        <el-table-column label="程序发送" prop="chengx"></el-table-column>
        <el-table-column label="检验" prop="jianyan"></el-table-column>
        <el-table-column label="入库" prop="ruku"></el-table-column>
      </el-table>
    </el-tab-pane>
    <el-tab-pane label="外协生产">外协生产</el-tab-pane>
  </el-tabs>

</template>

<script>
export default {
  name: "ProductionManagement",
  data() {
    return {
      toolOrderItemManagementVOS: [],
    }
  },
  mounted() {
    this.$http.get("tool/showAllItemForProduction").then(response => {
      this.toolOrderItemManagementVOS = this.processData(response.data.data);
    })
  },
  methods: {
    processData(data) {
      // 首先按图号排序
      data.sort((a, b) => a.drawingNumber.localeCompare(b.drawingNumber));

      // 计算每个图号的总数量并初始化每个项目
      return data.map(item => ({
        ...item,
        totalNumber: data.filter(x => x.drawingNumber === item.drawingNumber).reduce((acc, curr) => acc + curr.number, 0),
        productionBatch: item.productionBatch || ''  // 确保每个项目都有 productionBatch 属性
      }));
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (column.property === 'drawingNumber' || column.property === 'totalNumber' || column.property === 'productionBatch') {
        if (rowIndex === 0 || this.toolOrderItemManagementVOS[rowIndex].drawingNumber !== this.toolOrderItemManagementVOS[rowIndex - 1].drawingNumber) {
          const count = this.toolOrderItemManagementVOS.slice(rowIndex).filter(item => item.drawingNumber === row.drawingNumber).length;
          return [count, 1];
        } else {
          return [0, 0];
        }
      }
      return [1, 1];
    }
  }
}
</script>


<style scoped>

</style>